package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Receiving;
import ysq.xyg.model.Recharge;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReceivingInfo;
import ysq.xyg.model.info.RechargeInfo;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.ItemService;
import ysq.xyg.service.RechargeService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * 购物车
 *
 * @author congcong tao
 * @create 2018/8/9 下午11:01
 */
@Controller
@RequestMapping("/recharge")
public class RechargeController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(RechargeController.class);
    @Resource
    private RechargeService rechargeService;
    @Resource
    private ItemService itemService;

    private Recharge recharge;

    @RequestMapping("/listPage")
    public ModelAndView list(String status){
        ModelAndView view = new ModelAndView("/rechargeList");
        view.addObject("status", status);
        return view;
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Recharge recharge, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Recharge> pageInfo = rechargeService.pageInfo(recharge, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (Recharge item : pageInfo.getList()) {
                row = new JSONObject();
                row.put("id", item.getId());
                row.put("memberName", item.getMemberName());
                row.put("money", item.getMoney());
                row.put("status", item.getStatus());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id){
        ModelAndView view = new ModelAndView("/RechargeUpdateOrSave");
        recharge = rechargeService.selectByPrimaryKey(id);
        view.addObject("recharge", recharge);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(RechargeInfo rechargeInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        recharge = new Recharge();
        recharge.setId(BaseUtil.uId());
        recharge.setImage(rechargeInfo.getImage());
        recharge.setMemberId(rechargeInfo.getMemberId());
        recharge.setMemberName(rechargeInfo.getMemberName());
        recharge.setMoney(rechargeInfo.getMoney());
        recharge.setStatus("1");
        recharge.setDescription(rechargeInfo.getDescription());
        recharge.setCreateTime(new Date());
        int id = rechargeService.insert(recharge);
        if(id <= 0){
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    /**
     * 确认
     */
    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateStatus(RechargeInfo rechargeInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        recharge = rechargeService.selectByPrimaryKey(rechargeInfo.getId());
        recharge.setStatus(rechargeInfo.getStatus());
        recharge.setDescription(rechargeInfo.getDescription());
        int id = rechargeService.updateByPrimaryKey(recharge);
        if (id <= 0) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(id)) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int ids = rechargeService.deleteByPrimaryKey(id);
            if (ids <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }
}
